package com.igoal.admin.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.igoal.admin.web.model.AdminCommentsModel;
import com.igoal.dao.CommentDAO;
import com.igoal.dao.CommentEntryDAO;
import com.igoal.dao.CommentEntryDoneDAO;
import com.igoal.entity.Comment;
import com.igoal.entity.CommentEntry;
import com.igoal.entity.CommentEntryDone;
import com.igoal.entity.ComponentEnum;
import com.igoal.entity.DataStatus;
import com.igoal.entity.User;

@Controller
@RequestMapping("/admin/comments.html")
@Secured("ROLE_ADMIN")
public class TransferCommentsController extends SimpleFormController {
	@Autowired
	private CommentEntryDAO commentEntryDAO;
	
	@Autowired
	private CommentEntryDoneDAO commentEntryDoneDAO;
	
	@Autowired
	private CommentDAO commentDAO;
	
	private final int MOVE_COMMENTS_ACTION = 1000;
	
	public TransferCommentsController() {
		setCommandName("commentCommand");
		setCommandClass(AdminCommentsModel.class);
		setFormView("admin/comments");
		setSuccessView("redirect:/admin/comments.html");	
	}
	
	@Override
	@Transactional(readOnly=false)
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		AdminCommentsModel model = (AdminCommentsModel) command;
		if(MOVE_COMMENTS_ACTION == model.getAction()){
			List<Comment> comments = new ArrayList<Comment>();
			
			List<CommentEntry> entryComments = commentEntryDAO.getAll();
			for(CommentEntry commentEntry : entryComments){
				User commentOwner = commentEntry.getUser();
				Long componentId = commentEntry.getEntry().getId();
				String body = commentEntry.getComment();
				Date createDate = commentEntry.getCreateDate();
				Date updateDate = commentEntry.getUpdateDate();
				DataStatus status = commentEntry.getStatus();
				
				Comment comment = buildComment(commentOwner, componentId, ComponentEnum.GOAL_ENTRY, body, createDate, updateDate, status);
				logger.info(comment.toString());
				comments.add(comment);
			}
			
			List<CommentEntryDone> entryDoneComments = commentEntryDoneDAO.getAll();
			for(CommentEntryDone commentEntry : entryDoneComments){
				User commentOwner = commentEntry.getUser();
				Long componentId = commentEntry.getEntry().getId();
				String body = commentEntry.getComment();
				Date createDate = commentEntry.getCreateDate();
				Date updateDate = commentEntry.getUpdateDate();
				DataStatus status = commentEntry.getStatus();
				
				Comment comment = buildComment(commentOwner, componentId, ComponentEnum.DONE_ENTRY, body, createDate, updateDate, status);
				logger.info(comment.toString());
				comments.add(comment);
			}
						
			commentDAO.addComments(comments);
		}
		
		return new ModelAndView(getSuccessView());
	}
	
	private Comment buildComment(User owner, Long componentId, ComponentEnum component, String body, Date createDate, Date updateDate, DataStatus status){
		Comment comment = new Comment();
		comment.setUser(owner);
		comment.setComponentId(componentId);
		comment.setComponent(component);
		comment.setBody(body);
		comment.setCreateDate(createDate);
		comment.setUpdateDate(updateDate);
		comment.setStatus(status);
		return comment;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new AdminCommentsModel();
	}
}
